def load_json(file_path):
    import json
    with open(file_path,'r',encoding='utf-8') as file:
        return json.load(file)
               
def save_json(file_path,dict):
    import json
    with open(file_path,'w',encoding='utf-8') as file:
        json.dump(dict,file,ensure_ascii=False,indent=4)  
        
def change_attendance_of_student(scale_value,dict,
                                 selected_indices,
                                 label_student_id,
                                 label_chinese_name,
                                 label_english_name,
                                 label_group_number,
                                 label_absent_or_present,
                                 label_photo):
    from PIL import Image, ImageTk
    
    if selected_indices:  
        index = selected_indices[0]
        student = list(dict.keys())[index]  
        
    if scale_value == 0:
        dict[student]['absent_or_present']='Absent'
    elif scale_value == 1:
        dict[student]['absent_or_present']='Present'
        
    photo_path_get = photo_path(selected_indices,dict)
    img = Image.open(photo_path_get)
    img = img.resize((100, 100))
    photo = ImageTk.PhotoImage(img)
    label_photo.config(image=photo)
    label_photo.image = photo
  
    label_student_id.config(text=f"student id:{dict[student]['student_id']}")       
    label_chinese_name.config(text=f"chinese name:{dict[student]['chinese_name']}") 
    label_english_name.config(text=f"english name:{dict[student]['english_name']}")
    label_group_number.config(text=f"group number:{dict[student]['group_number']}")
    label_absent_or_present.config(text=dict[student]['absent_or_present'])
    

def button_sum(scale_value,selected_indices,dict,
                                 label_student_id,                                 
                                 label_chinese_name,
                                 label_english_name,
                                 label_group_number,
                                 label_absent_or_present,
                                 label_photo):
    change_attendance_of_student(scale_value,dict,selected_indices,
                                 label_student_id,
                                 label_chinese_name,
                                 label_english_name,
                                 label_group_number,
                                 label_absent_or_present,
                                 label_photo)
    file_path = 'student_information/student_information.json'
    save_json(file_path,dict)

def on_button_click_check(selected_indices,
                    dict,
                    label_student_id,
                    label_chinese_name,
                    label_english_name,
                    label_group_number,
                    label_absent_or_present,
                    label_photo
                    ):
    from function.window_function import photo_path
    from PIL import Image, ImageTk
    
    if selected_indices:  
        index = selected_indices[0]
        student = list(dict.keys())[index]  
        
    photo_path_get = photo_path(selected_indices,dict)
    img = Image.open(photo_path_get)
    img = img.resize((100, 100))
    photo = ImageTk.PhotoImage(img)
    label_photo.config(image=photo)
    label_photo.image = photo

    label_student_id.config(text=f"student id:{dict[student]['student_id']}")       
    label_chinese_name.config(text=f"chinese name:{dict[student]['chinese_name']}") 
    label_english_name.config(text=f"english name:{dict[student]['english_name']}")
    label_group_number.config(text=f"group number:{dict[student]['group_number']}")
    label_absent_or_present.config(text=dict[student]['absent_or_present'])
     
def photo_path(selected_indices,dict):
    
    if selected_indices:  
        index = selected_indices[0]
        student = list(dict.keys())[index]  
    photo_id = dict[student]["photo"]
    return 'student_information/photo/'+ photo_id


def save_as_CSV(file_path,dict):
    import csv
    fieldnames =["photo",
        "student_id",
        "chinese_name",
        "english_name",
        "group_number",
        "absent_or_present"]
    with open(file_path, mode='w', newline='', encoding='utf-8') as file:
    
        writer = csv.DictWriter(file, fieldnames=fieldnames)
        writer.writeheader()
        
        for student_id, student_info in dict.items():
            row = {"student_id": student_id}
            row.update(student_info)
            writer.writerow(row)
            